#include "Collision.h"

Collision::Collision()
{
}

void Collision::TwoObject(Object* obj1, Object* obj2, float gameTime)
{
	RECT bond = obj1->GetIntersectBond(obj2);
	// For object 1
	RECT rect1 = obj1->GetBond();
	// Xet truong hop va cham duoi (bottom)
	if ((((rect1.left == bond.left) && (rect1.right != bond.right)) || ((rect1.right == bond.right) && (rect1.left != bond.left)))
		&& (bond.right - bond.left < bond.bottom - bond.top)) // Phai o tren cam doidddddddddddddddddddd
	{
		 
		 
		 
	}
	else if (rect1.bottom == bond.bottom)
	{ 
		obj1 =  new Object(30,30);


	}
	else if (rect1.top == bond.top)
	{
		 
		 
	}

	 
}

void Collision::TwoObject2(Object* obj1, Object* obj2, float gameTime)
{
	RECT bond = obj1->GetIntersectBond(obj2);
	// For object 1
	RECT rect1 = obj1->GetBond();
	if ((((rect1.left == bond.left) && (rect1.right != bond.right)) || ((rect1.right == bond.right) && (rect1.left != bond.left)))
		&& (bond.right - bond.left < bond.bottom - bond.top)) // Phai o tren cam doidddddddddddddddddddd
	{
		obj1->m_collisionResponse += D3DXVECTOR2(1000, 0);
	}
	else if (rect1.bottom == bond.bottom)
	{
		obj1->m_collisionResponse += D3DXVECTOR2(0, -1000);
	}
	else if (rect1.top == bond.top)
	{
		 
		 
	}

	 
}

bool Collision::RectVsRect(RECT r1, RECT r2)
{
	return (r1.left<r2.right&&r1.right>r2.left&&r1.top<r2.bottom&&r1.bottom>r2.top);
}
bool Collision::RectVsRect(RECT r1Old, RECT r1, RECT r2Old,RECT r2,int differential)
{
	float deltaX1=(r1.left-r1Old.left)/differential;
	float deltaY1=(r1.top-r1Old.top)/differential;
	float deltaX2=(r2.left-r2Old.left)/differential;
	float deltaY2=(r2.top-r2Old.top)/differential;

	for(char i=0;i<=differential;i++)
	{
		RECT r1New, r2New;
		r1New.left=r1Old.left+deltaX1*i;
		r1New.right=r1Old.right+deltaX1*i;
		r1New.top=r1Old.top+deltaY1*i;
		r1New.bottom=r1Old.bottom+deltaY1*i;
		r2New.left=r2Old.left+deltaX2*i;
		r2New.right=r2Old.right+deltaX2*i;
		r2New.top=r2Old.top+deltaY2*i;
		r2New.bottom=r2Old.bottom+deltaY2*i;
		bool checkCollision=RectVsRect(r1New,r2New);
		if(checkCollision)
			return checkCollision;
	}
	return 0;
}